03 / 14

How do you loop through an array?

There are many ways in which we can loop through an array:

  1. 1

    forEach: available on array instance

  2. 2

    for

  3. 3

    for in

  4. 4

    for of

  5. 5

    map: available on array instance

  6. 6

    reduce: available on array instance

The forEach() method of Array instances executes a provided function once for each array element.
The for loop
The for...in statement iterates over all enumerable string properties of an object (ignoring properties keyed by symbols), including inherited enumerable properties.
The for...of statement executes a loop that operates on a sequence of values sourced from an iterable object. Iterable objects include instances of built-ins such as Array, String, TypedArray, Map, Set, NodeList (and other DOM collections), as well as the arguments object, generators produced by generator functions, and user-defined iterables.
The map() method of Array instances creates a new array populated with the results of calling a provided function on every element in the calling array.
The reduce() method of Array instances executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.
Difficulty: 2/10
Topics: for loops, Array iteration methods, performance considerations

Scenario Questions

0-2 years experience
  1. 1

    Given an array of numbers, write a function that returns a new array with each value doubled using a loop.

  2. 2

    How would you iterate over an array of user objects to log each user's email to the console?

  3. 3

    If you need to stop iterating once you find a matching element, how would you modify your loop?

2-5 years experience
  1. 1

    We have a legacy codebase that uses a for...i loop to process items, but we need to refactor it to use modern iteration methods. What trade‑offs would you consider, and how would you ensure behavior stays the same?

  2. 2

    During debugging you notice that a loop over a large array sometimes skips elements when the array contains undefined holes. What could cause that, and how would you fix it?

  3. 3

    Our feature needs to process items in batches of 10. How would you structure the loop to handle batching and early exit on error?

5-8 years experience
  1. 1

    When processing a million‑record array in the browser, what looping strategy would you choose to avoid UI jank, and why?

  2. 2

    Explain how you would implement a custom iterator for a complex data structure that needs to be looped with for…of, considering memory and performance.

  3. 3

    If multiple components need to share a large immutable array, how would you design the iteration to minimize garbage collection pressure?

8+ years experience
  1. 1

    Our service ingests streams of events and currently materializes them into large arrays before processing. How would you redesign the system to handle unbounded data without relying on full in‑memory loops?

  2. 2

    When migrating a monolithic codebase to a micro‑frontend architecture, how would you standardize array iteration patterns across teams to reduce bugs and improve maintainability?

  3. 3

    Discuss the trade‑offs of using synchronous loops versus async/await pipelines for processing data across distributed services.

Follow-up Questions

  • What would happen if the array contains a non-numeric value in your doubling function?
  • How does using forEach affect the ability to break out of the loop early?
  • Can you compare the memory profile of a for…of loop versus a traditional indexed for loop?